package com.snut.tomorrowNews.service.back;

import com.snut.tomorrowNews.dao.back.NewsDao;
import com.snut.tomorrowNews.model.News;
import com.snut.tomorrowNews.model.NewsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NewsService {

    @Autowired
    NewsDao newsDao;

    public List<News> getNewsList() {
        return newsDao.newsList();
    }

    public List<NewsType> getNewsTypeList() {
        return newsDao.newsTypeList();
    }

    /**
     * 保存新闻
     */
    public void addNews(News news) {
        newsDao.insertNews(news);
    }

    /**
     * 删除新闻，先删收藏
     */
    public void deleteNews(Integer id) {
        newsDao.deleteCollectByNewsId(id);
        newsDao.deleteNewsById(id);
    }

    public Map<String, List> statisticsByType() {
        Map<String, List> map = new HashMap<>();
        List<String> typeList = newsDao.findTypeName();
        List<Integer> countList = newsDao.countByTypeList();
        map.put("x", typeList);
        map.put("y", countList);
        return map;
    }

    public News getNewsById(Integer newsId) {
        return newsDao.getNewsById(newsId);
    }
}
